package com.example.demo.utils;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private Integer code; // 编码：200成功，500和其它数字为失败
    private String msg; // 错误信息
    private T data; // 数据

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result<Void> success() {
        Result<Void> result = new Result<>();
        result.code = 200;
        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<>();
        result.data = object;
        result.code = 200;
        return result;
    }

    public static <T> Result<T> success(String msg, T object) {
        Result<T> result = new Result<>();
        result.data = object;
        result.code = 200;
        result.msg = msg;
        return result;
    }

    // 修改error方法，不需要泛型参数
    public static Result<Void> error(String msg) {
        Result<Void> result = new Result<>();
        result.msg = msg;
        result.code = 500;
        return result;
    }

    // 修改error方法，不需要泛型参数
    public static Result<Void> error(Integer code, String msg) {
        Result<Void> result = new Result<>();
        result.msg = msg;
        result.code = code;
        return result;
    }
}
